Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bytes::Bytes as the HTTP request body in HttpClient. #2485

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mstyura
Copy link

@mstyura mstyura commented Dec 29, 2024

This will allow pooling of buffers on caller side compared to usage of Vec.

Fixes

Lack of possibility to reuse buffers which are consumed by HttpClient after request has been sent.

Design discussion issue

Should old function be kept with deprecated annotation and hence new function had to have a different name or should I just introduce breaking change by changing signature of a function?

Changes

I propose to change type of body of http request accepted by HttpClient trait from Vec<u8> to bytes::Bytes.
This should unblock pooling of buffers which can be used both for serialization & being http request bodies.
Something like this (pseudo-code to get the idea):

async fn export(&mut self, batch: Vec<SpanData>) -> ExportResult  {
    let mut buffer = pool.get();
    let request = self.build_request(&mut buffer, batch);
    let res = send_request(client, request);
    pool.return(buffer);
    res
}

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@mstyura mstyura requested a review from a team as a code owner December 29, 2024 09:28
Copy link

codecov bot commented Dec 29, 2024

Codecov Report

Attention: Patch coverage is 0% with 15 lines in your changes missing coverage. Please review.

Project coverage is 77.9%. Comparing base (a1dda22) to head (c643683).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
opentelemetry-http/src/lib.rs 0.0% 6 Missing ⚠️
opentelemetry-otlp/src/exporter/http/logs.rs 0.0% 2 Missing ⚠️
opentelemetry-otlp/src/exporter/http/metrics.rs 0.0% 2 Missing ⚠️
opentelemetry-otlp/src/exporter/http/trace.rs 0.0% 2 Missing ⚠️
opentelemetry-zipkin/src/exporter/uploader.rs 0.0% 2 Missing ⚠️
...try-sdk/src/trace/sampler/jaeger_remote/sampler.rs 0.0% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##            main   #2485     +/-   ##
=======================================
- Coverage   77.9%   77.9%   -0.1%     
=======================================
  Files        123     123             
  Lines      22888   22891      +3     
=======================================
  Hits       17839   17839             
- Misses      5049    5052      +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mstyura
Copy link
Author

mstyura commented Jan 2, 2025

Hello @cijothomas
Could you please take a look at this PR once you have time?
Thanks a lot in advance!

This will allow pooling of buffers on caller side compared to usage of Vec<u8>.
@mstyura mstyura force-pushed the http-client-request-body-bytes branch from 2e19563 to e0156e6 Compare January 2, 2025 11:22
///
/// Returns an error if it can't connect to the server or the request could not be completed,
/// e.g. because of a timeout, infinite redirects, or a loss of connection.
async fn send_bytes(&self, request: Request<Bytes>) -> Result<Response<Bytes>, HttpError>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so I understand you want to create a pool of Bytes and reuse them across different send/send_bytes call?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly, serialization of spans to export and send to Datadog is one of the largest memory allocation source of backend service I'm working on. I want to reuse buffers to serialize and sending over http between calls to exports.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mstyura - will this enable buffer reuse in opentelemetry-otlp as well? It doesn't seem to be part of this PR, so ensuring.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lalitb I didn't have such plans initially, as I see proposed API is used by oltp and zipkin inside this repo, so they could leverage as well from buffer pooling. But as I don't use them in production I don't understand how heavily they actually allocate memory compared to DataDog exported I initially wanted to fix.
Do you want me to implement pooling for oltp and zipkin? If so I think it's preferable to do it as separate MR.

@@ -28,7 +28,7 @@ impl LogExporter for OtlpHttpClient {
.method(Method::POST)
.uri(&self.collector_endpoint)
.header(CONTENT_TYPE, content_type)
.body(body)
.body(body.into())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this simply takes ownership of the Vec's buffer and wraps it in a Bytes object? Just want to ensure there is no copy operation here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants